home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / Checker / Checker.cs next >
Encoding:
Text File  |  2002-04-18  |  2.2 KB  |  67 lines

  1. //--------------------------------------
  2. // Checker.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class Checker: Form
  9. {
  10.      protected const int     xNum = 5;       // N·mero de cuadros horizontales
  11.      protected const int     yNum = 4;       // N·mero de cuadros verticales
  12.      protected       bool[,] abChecked = new bool[yNum, xNum];
  13.      protected       int     cxBlock, cyBlock;
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new Checker());
  18.      }
  19.      public Checker()
  20.      {
  21.           Text = "Marcador";
  22.           BackColor = SystemColors.Window;
  23.           ForeColor = SystemColors.WindowText;
  24.           ResizeRedraw = true;
  25.  
  26.          OnResize(EventArgs.Empty);
  27.      }
  28.      protected override void OnResize(EventArgs ea)
  29.      {
  30.           base.OnResize(ea);            // Sino, ResizeRedraw no funciona
  31.  
  32.           cxBlock = ClientSize.Width  / xNum;
  33.           cyBlock = ClientSize.Height / yNum;
  34.      }
  35.      protected override void OnMouseUp(MouseEventArgs mea)
  36.      {
  37.           int x = mea.X / cxBlock;
  38.           int y = mea.Y / cyBlock;
  39.  
  40.           if (x < xNum && y < yNum)
  41.           {
  42.                abChecked[y, x] ^= true;
  43.                Invalidate(new Rectangle(x * cxBlock, y * cyBlock,
  44.                                         cxBlock, cyBlock));
  45.           } 
  46.      }
  47.      protected override void OnPaint(PaintEventArgs pea)
  48.      {
  49.           Graphics grfx = pea.Graphics;
  50.           Pen      pen  = new Pen(ForeColor);
  51.  
  52.           for (int y = 0; y < yNum; y++)
  53.           for (int x = 0; x < xNum; x++)
  54.           {
  55.                grfx.DrawRectangle(pen, x * cxBlock, y * cyBlock, 
  56.                                        cxBlock, cyBlock);
  57.                if (abChecked[y, x])
  58.                {
  59.                     grfx.DrawLine(pen, x      * cxBlock,  y      * cyBlock,
  60.                                       (x + 1) * cxBlock, (y + 1) * cyBlock);
  61.                     grfx.DrawLine(pen, x      * cxBlock, (y + 1) * cyBlock,
  62.                                       (x + 1) * cxBlock,  y      * cyBlock);
  63.                }
  64.           }
  65.      }
  66.  }
  67.